﻿using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using WeChat.Common;
using System.Configuration;
namespace WeChat
{
    /// <summary>
    /// Successfulpayment 的摘要说明
    /// </summary>
    public class Successfulpayment : IHttpHandler, IRequiresSessionState
    {
        string publickey = "";
        public void ProcessRequest(HttpContext context)
        {
            string num = "";
            Secretkey();
            //opid值
            string opId = context.Session["openid"] == null ? "" : context.Session["openid"].ToString();
            //操作员
            string tn = context.Session["tn"] == null ? "" : context.Session["tn"].ToString();

            string business_no = context.Session["business_no"] == null ? "" : context.Session["business_no"].ToString();
            //通过opid查询卡号
            string ryid = ReBLL.SelectRyid(opId);
            //获取订单号
            string o = ReBLL.OrderRyid(ryid);
            string json = "{\"a\":\"payChk\",\"o\":\"" + o + "\",\"tn\":\"" + tn + "\"}";
            string returnTxt = RsaDemo.Common.HttpPost("s=" + RsaDemo.Common.rsaEncode(publickey, json, business_no));
            IDictionary<string, object> rlt = RsaDemo.Common.JsonToDictionary(returnTxt);
            if (rlt["f"].ToString() == "1" && business_no != "")
            {
                num = "1";
                context.Session.Remove("tn");
                context.Session.Remove("business_no");
            }
            context.Response.Write(num);
        }
        //获取密钥
        public void Secretkey()
        {
#pragma warning disable CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
            string business_no = ConfigurationSettings.AppSettings["business_no"];
#pragma warning restore CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
#pragma warning disable CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
            string sn = ConfigurationSettings.AppSettings["sn"];
#pragma warning restore CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
            //此处自行验证各值合法性
            string json = "{\"a\":\"getKey\",\"e\":\"" + business_no + "\",\"sn\":\"" + sn + "\"}";
            string returnTxt = RsaDemo.Common.HttpPost("json=" + json);
            IDictionary<string, object> rlt = RsaDemo.Common.JsonToDictionary(returnTxt);
            if (rlt["f"].ToString() == "1")
            {
                IDictionary<string, object> d = (IDictionary<string, object>)rlt["d"];
                //this.richTextBox1.Text = d["k"].ToString();
                publickey = d["k"].ToString();
                //this.textBox5.Text = "0001";
            }
            else
            {
                //MessageBox.Show(rlt["m"].ToString());
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}